<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="python,脚本," />








  <link rel="shortcut icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="最近入手了一个kindle，不想在亚马逊上买电子书，就在电脑上下载了一堆电子书，kindle使用USB连接电脑就可以将电子书导入到kindle中，但是这样一个是格式不一，另一个是不好管理，而使用邮箱向kindle中发送主题为“convert”的邮件，亚马逊会自动转换适合kindle的格式，并保存在对应账号的个人文档中。缺点是每次附件只能添加一本书籍多了不识别  于是我就想着写个脚本来批量处理(基">
<meta name="keywords" content="python,脚本">
<meta property="og:type" content="article">
<meta property="og:title" content="python脚本批量上传kindle电子书">
<meta property="og:url" content="http://yoursite.com/2017/09/13/python脚本批量上传kindle电子书/index.html">
<meta property="og:site_name" content="闫 通 亮 的 博 客">
<meta property="og:description" content="最近入手了一个kindle，不想在亚马逊上买电子书，就在电脑上下载了一堆电子书，kindle使用USB连接电脑就可以将电子书导入到kindle中，但是这样一个是格式不一，另一个是不好管理，而使用邮箱向kindle中发送主题为“convert”的邮件，亚马逊会自动转换适合kindle的格式，并保存在对应账号的个人文档中。缺点是每次附件只能添加一本书籍多了不识别  于是我就想着写个脚本来批量处理(基">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/7825391-5c386536d53f5b87.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/7825391-2b3a4943dd593015.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/7825391-57aca4502a5564b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2017-09-20T05:51:21.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="python脚本批量上传kindle电子书">
<meta name="twitter:description" content="最近入手了一个kindle，不想在亚马逊上买电子书，就在电脑上下载了一堆电子书，kindle使用USB连接电脑就可以将电子书导入到kindle中，但是这样一个是格式不一，另一个是不好管理，而使用邮箱向kindle中发送主题为“convert”的邮件，亚马逊会自动转换适合kindle的格式，并保存在对应账号的个人文档中。缺点是每次附件只能添加一本书籍多了不识别  于是我就想着写个脚本来批量处理(基">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/7825391-5c386536d53f5b87.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/09/13/python脚本批量上传kindle电子书/"/>





  <title>python脚本批量上传kindle电子书 | 闫 通 亮 的 博 客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">闫 通 亮 的 博 客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/09/13/python脚本批量上传kindle电子书/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="闫通亮">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="闫 通 亮 的 博 客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">python脚本批量上传kindle电子书</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-13T20:46:25+08:00">
                2017-09-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>最近入手了一个kindle，不想在亚马逊上买电子书，就在电脑上下载了一堆电子书，kindle使用USB连接电脑就可以将电子书导入到kindle中，但是这样一个是格式不一，另一个是不好管理，而使用邮箱向kindle中发送主题为“convert”的邮件，亚马逊会自动转换适合kindle的格式，并保存在对应账号的个人文档中。缺点是每次附件只能添加一本书籍多了不识别</p>
</blockquote>
<p>于是我就想着写个脚本来批量处理(基于python3)</p>
<h3 id="配置信息"><a href="#配置信息" class="headerlink" title="配置信息"></a>配置信息</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 输入Email地址和口令:</span></div><div class="line">from_addr = <span class="string">'2207963631@qq.com'</span></div><div class="line">password = <span class="string">'点击生成授权码生成， 不是qq密码'</span></div><div class="line"><span class="comment"># 输入收件人地址:</span></div><div class="line">kindle_addr = <span class="string">'17682348771@kindle.cn'</span></div><div class="line"><span class="comment"># 输入SMTP服务器地址:</span></div><div class="line">smtp_server = <span class="string">'smtp.qq.com'</span></div><div class="line"><span class="comment"># 默认要上传kindle电子书的文件夹</span></div><div class="line">default_dir = <span class="string">'/Users/yantongliang/Desktop/PDF书籍/kindle'</span></div></pre></td></tr></table></figure>
<p>这里我使用的是qq邮箱，要在设置-账户 中开启POP3/SMTP服务。</p>
<p><img src="http://upload-images.jianshu.io/upload_images/7825391-5c386536d53f5b87.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="qq邮箱 设置-账户 中开启POP3/SMTP服务"><br>点击生成授权码， 根据提示 发送短信 获取到授权码 使用该授权码作为密码发送邮件。<br>smtp_server（发件服务器） 为’smtp.qq.com’ 不需要改动<br>default_dir（我存放kindle的文件夹）下载好的电子书我在这个路径下保存着</p>
<h3 id="构造邮件msg"><a href="#构造邮件msg" class="headerlink" title="构造邮件msg"></a>构造邮件msg</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">getMsg</span><span class="params">(filePath)</span>:</span></div><div class="line">    msg = MIMEMultipart()</div><div class="line">    msg[<span class="string">'From'</span>] = _format_addr(from_addr)</div><div class="line">    msg[<span class="string">'To'</span>] = _format_addr(kindle_addr)</div><div class="line">    msg[<span class="string">'Subject'</span>] = Header(<span class="string">'convert'</span>, <span class="string">'utf-8'</span>).encode()</div><div class="line"></div><div class="line">    <span class="comment"># ---这是邮件文字部分---</span></div><div class="line">    part = MIMEText(<span class="string">'使用python 发送的电子书'</span>)</div><div class="line">    msg.attach(part)</div><div class="line"></div><div class="line">    part = MIMEApplication(open(filePath, <span class="string">'rb'</span>).read())</div><div class="line">    fileName = filePath.split(<span class="string">'/'</span>)[<span class="number">-1</span>]</div><div class="line">    print(fileName)</div><div class="line">    part.add_header(<span class="string">'Content-Disposition'</span>, <span class="string">'attachment'</span>, filename=fileName)</div><div class="line">    msg.attach(part)</div><div class="line">    <span class="keyword">return</span> msg</div></pre></td></tr></table></figure>
<p>使用这个方法，传入文件的路径，来构造一个邮件对象。 然后将这个对象发送出去就完成了。<br>其中 msg[‘Subject’] 为邮件的主题 需要设为“ convert” </p>
<h3 id="获取文件并上传"><a href="#获取文件并上传" class="headerlink" title="获取文件并上传"></a>获取文件并上传</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    server = smtplib.SMTP_SSL(smtp_server, <span class="number">465</span>) <span class="comment">#连接smtp邮件服务器</span></div><div class="line">    server.login(from_addr, password)   <span class="comment">#登录到服务器</span></div><div class="line">    </div><div class="line">    <span class="keyword">if</span> len(argv) &lt; <span class="number">2</span>:</div><div class="line">        print(<span class="string">'发送默认文件夹中的文件'</span>)</div><div class="line">        list = os.listdir(default_dir)</div><div class="line">        <span class="keyword">for</span> file <span class="keyword">in</span> list:</div><div class="line">            <span class="keyword">if</span> file[<span class="number">0</span>] != <span class="string">'.'</span> <span class="keyword">and</span> file[<span class="number">0</span>] != <span class="string">'*'</span>:   <span class="comment"># 文件以 . 开头的 则是不需要上传的隐藏文件</span></div><div class="line">                filePath = os.path.join(default_dir,file)</div><div class="line">                msg = getMsg(filePath)</div><div class="line">                server.sendmail(from_addr, kindle_addr, msg.as_string())</div><div class="line">                os.renames(filePath,os.path.join(default_dir,<span class="string">'*'</span>+file))</div><div class="line"></div><div class="line">    <span class="keyword">else</span>:</div><div class="line">        msg = getMsg(argv[<span class="number">1</span>])</div><div class="line">        server.sendmail(from_addr, kindle_addr, msg.as_string())</div><div class="line"></div><div class="line">    server.quit()</div></pre></td></tr></table></figure>
<ul>
<li>使用qq邮箱的服务器地址“smtp.qq.com” 和端口“465” 连接到邮箱服务器</li>
<li>使用qq邮箱账号和密码（授权码）来登录到服务器</li>
<li>case1 ：传了文件路径，则使用该文件路径获取邮件对象，并发送邮件<br>case2：没传任何参数则，将默认文件夹中的文件都传到kindle邮箱中。（这里我将传送完毕的文件名 前面加上前缀“*”，来标识已传输）</li>
<li>最后 关闭邮箱服务器</li>
</ul>
<p><a href="https://github.com/YanTongLiang/demo/blob/master/Python/toKindle.py" target="_blank" rel="external">代码放在github上(https://github.com/YanTongLiang/demo/blob/master/Python/toKindle.py)</a></p>
<h3 id="备注：可能会遇到的问题"><a href="#备注：可能会遇到的问题" class="headerlink" title="备注：可能会遇到的问题"></a>备注：可能会遇到的问题</h3><ol>
<li><p>python抛出的异常。由于发送邮件太频繁。。（大概连续发送了50多本电子书）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">smtplib.SMTPDataError: (550, b&apos;Connection frequency limited&apos;)</div></pre></td></tr></table></figure>
</li>
<li><p>qq邮箱收到提示发送未成功（也可能是因为太频发）<br>可以通过点击填写验证码 填写完成自动重新发送<br><img src="http://upload-images.jianshu.io/upload_images/7825391-2b3a4943dd593015.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="qq邮箱提示"></p>
</li>
<li>部分电子书格式不支持</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/7825391-57aca4502a5564b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="您向 Kindle 发送的文件出现问题"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/python/" rel="tag"># python</a>
          
            <a href="/tags/脚本/" rel="tag"># 脚本</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/12/iOS常用宏定义/" rel="next" title="iOS常用宏 定义">
                <i class="fa fa-chevron-left"></i> iOS常用宏 定义
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <p class="site-author-name" itemprop="name">闫通亮</p>
            <p class="site-description motion-element" itemprop="description"></p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives/">
            
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置信息"><span class="nav-number">1.</span> <span class="nav-text">配置信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#构造邮件msg"><span class="nav-number">2.</span> <span class="nav-text">构造邮件msg</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取文件并上传"><span class="nav-number">3.</span> <span class="nav-text">获取文件并上传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#备注：可能会遇到的问题"><span class="nav-number">4.</span> <span class="nav-text">备注：可能会遇到的问题</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">闫通亮</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动</div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">主题 &mdash; <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.2</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  

  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  





  

  

  

  

  

  



</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
<script type="text/javascript" src="/js/src/bg.js"></script>


